package cn.org.zx.common.commutils;

import java.math.BigDecimal;
import java.text.DecimalFormat;
import java.text.NumberFormat;
import java.util.Locale;

import cn.org.zx.common.commutils.CommMethod;
@SuppressWarnings("unchecked")
public class StringUtils {
	
	private static int PRECISION = 2;
	private static String[] chrChineseUnit = { "分", "角", "元", "拾", "佰", "仟", "万", "拾", "佰", "仟", "亿", "拾", "佰", "仟",
		"兆", "拾", "佰", "仟" };
 
	private static String[] chrChineseNumber = { "零", "壹", "贰", "叁", "肆", "伍", "陆", "柒", "捌", "玖" };

	private static String chrChineseFull = "整";

	private static String chrChineseNegative = "负";	
	 
	/**
	 * 将一个字符串格式化为给定的长度，过长的话按照给定的长度从字符串左边截取，反之以给定的 字符在字符串右边补足空余位 <br>
	 * 比如： <br>
	 * suffixStr("abc",'0',5) 将返回 aaa00 <br>
	 * suffixStr("abc",'0',2) 将返回 ab
	 * @param source 原始字符串

	 * @param profix 补足空余位时使用的字符串
	 * @param length 格式化后字符串长度

	 * @return 返回格式化后的字符串,异常返回null
	 */
	public static String suffixStr(String source, char suffix, int length) {
		String strRet = source;
		if (source == null) {
			return strRet;
		}
		if (source.length() >= length) {
			strRet = source.substring(0, length);
		}

		if (source.length() < length) {
			for (int i = 0; i < length - source.length(); i++) {
				strRet += suffix;
			}
		}
		return strRet;
	}
	
	/**
	 * Description 将数字金额转换为中文金额
	 * @param
	 * BigDecimal bigdMoneyNumber 转换前的数字金额
	 * @return String 调用：stringToBig("101.89")="壹佰零壹圆捌角玖分" stringToBig("100.89")="壹佰零捌角玖分"
	 *         stringToBig("100")="壹佰圆整"
	 */
	public static String DoNumberCurrencyToChineseCurrency(BigDecimal bigdMoneyNumber) {
		StringBuffer sb = new StringBuffer();
		int signum = bigdMoneyNumber.signum();
		if(signum == 0){
			return "零元整";
		}

		long number = bigdMoneyNumber.movePointRight(PRECISION).setScale(0, BigDecimal.ROUND_HALF_UP).abs().longValue();

		long scale = number % 100;

		int numUnit = 0;
		int numIndex = 0;
		// 遇零标志
		boolean getZero = false;
		
		if (scale == 0) {
			numIndex = 2;
			number = number / 100;
			getZero = true;
		}
		if (scale != 0 && scale % 10 == 0) {
			numIndex = 1;
			number = number / 10;
			getZero = true;
		}
		int zeroSize = 0;
		while (number > 0) {
			numUnit = (int) (number % 10);
			if (numUnit > 0) {
				// 非零处理
				if (numIndex == 9 && zeroSize >= 3) {
					sb.insert(0, chrChineseUnit[6]);
				}
				if (numIndex == 13 && zeroSize >= 3) {
					sb.insert(0, chrChineseUnit[10]);
				}
				sb.insert(0, chrChineseUnit[numIndex]);
				sb.insert(0, chrChineseNumber[numUnit]);
				getZero = false;
				zeroSize = 0;
			} else {
				// 为零处理
				zeroSize++;
				if (!getZero)
					sb.insert(0, chrChineseNumber[numUnit]);

				if (numIndex == 2) {
					if (number > 0) {
						sb.insert(0, chrChineseUnit[numIndex]);
					}
				} else if ((numIndex - 2) % 4 == 0) {
					if (number % 1000 > 0) {
						sb.insert(0, chrChineseUnit[numIndex]);
					}
				}
				getZero = true;
			}

			// 自除10
			number = number / 10;
			numIndex++;
		}

		if(signum == -1){
			sb.insert(0, chrChineseNegative);
		}

		if(scale == 0){
			sb.append(chrChineseFull);
		}

		return sb.toString();
	}
	
	public static String stringToBig(String value) {
		String result = "零元整";
		if (null == value || "".equals(value.trim())) {
			return result;
		}

		try {
			BigDecimal b = new BigDecimal(value);
			result = DoNumberCurrencyToChineseCurrency(b);

		} catch (Exception e) {
			//
			return "零元整";
		}
		return result;

	}
	
	/**
	 * 将一串字符串按某一符合分割开
	 * strSplit("abcabbacc",'a') <br>
	 */
	public static String[] strSplit(String s, String s1)
    {
        int i = 0;
        int j = 0;
        @SuppressWarnings("unused")
		boolean flag = false;
        int i1 = s1.length();
        if(s.compareTo("") != 0)
        {
            if(s.indexOf(s1) != -1)
            {
                j = s.indexOf(s1);
                int j1 = 1;
                do
                {
                    if(s.indexOf(s1, j + i1) != -1)
                    {
                        j = s.indexOf(s1, j + i1);
                        i = j1;
                    } else
                    {
                        i += 2;
                        break;
                    }
                    j1++;
                } while(true);
            } else
            {
                i = 1;
            }
        } else
        {
            i = 0;
        }
        j = 0;
        flag = false;
        String as[] = new String[i];
        if(s.compareTo("") != 0)
        {
            if(s.indexOf(s1) != -1)
            {
                int k = s.indexOf(s1);
                as[0] = s.substring(0, k);
                int k1 = 1;
                do
                {
                    if(s.indexOf(s1, k + i1) != -1)
                    {
                        int l = s.indexOf(s1, k + i1);
                        as[k1] = s.substring(k + i1, l);
                        k = s.indexOf(s1, k + i1);
                    } else
                    {
                        as[k1] = s.substring(k + i1, s.length());
                        break;
                    }
                    k1++;
                } while(true);
            } else
            {
                as[0] = s.substring(0, s.length());
                return as;
            }
        } else
        {
            return as;
        }
        return as;
    }
	
	/**
	 * 将一串字符串中把某个字符替换成其它字符

	 * strReplace("abcabbacc",'a',"X") <br>
	 */
	public static String strReplace(String s, String s1, String s2)
    {
        String as[] = strSplit(s, s1);
        String s3 = null;
        if(as.length != 0)
        {
            s3 = as[0];
            for(int i = 0; i < as.length - 1; i++)
                s3 = CommMethod.strDealNull(s3) + s2 + as[i + 1];

        }
        return CommMethod.strDealNull(s3);
    } 
	
	/**
	 * 将一个浮点数转换为人民币的显示格式：￥##,###.##
	 *  
	 * @param value
	 *            浮点数

	 * @return 人民币格式显示的数字
	 */
	public static String toRMB(double value) {
		NumberFormat nf = NumberFormat.getCurrencyInstance(Locale.CHINA);
		return nf.format(value);
	}
	
	/**
	 * 默认保留小数点后两位，将一个浮点数转换为定长小数位的小数 ######.##
	 * @param value  浮点数

	 * @return 定长小数位的小数
	 */
	public static String toCurrencyWithoutComma(double value) {
		String retValue = toCurrency(value);
		retValue = retValue.replaceAll(",", "");
		return retValue;
	}
	
	/**
	 * 默认保留小数点后两位，将一个浮点数转换为货币的显示格式：##,###.##
	 * @param value  浮点数

	 * @return 货币格式显示的数字

	 */
	public static String toCurrency(double value) {
		DecimalFormat format = new DecimalFormat("##,##0.00");
		return format.format(value);
		//return toCurrency(value);
	}
	
	public static String toCurrencyNoPoint(long value) {
		DecimalFormat format = new DecimalFormat("###,###");
		return format.format(value );
		//return toCurrency(value);
	}
	
	public static String toString(Object obj){
		String strReturn = "";
		if(obj == null){
			strReturn = CommMethod.strDealNull(strReturn);
		}else if("String[]".equals(obj.getClass().getSimpleName())){
			String[] str = (String[])obj;
			if(str.length>0){
				strReturn = str[0];
			}
		}else{
			strReturn = obj.toString();
		}
		
		
		
		return strReturn.trim();
	}

	

	
	/**
	 * 把一个字符串类型的数字 转成带 数字符号的  但是如果为""则返回""
	 */
	public static String paersDoubleString(String temp){
		if(null == temp || "".equals(temp))return "";
		else return StringUtils.toCurrency(Double.parseDouble(temp));
	}
	/**
	 * 把一个字符串类型的数字 转成带 数字符号的  但是如果为""则返回""
	 */
	public static String paersIntString(String temp){
		if(null == temp || "".equals(temp))return "";
		else return StringUtils.toCurrencyNoPoint(Integer.parseInt(temp));
	}
	/**
	 * 把一个字符串类型的数字 转成带 数字符号的  但是如果为""则返回""
	 */
	public static String paersLongString(String temp){
		if(null == temp || "".equals(temp))return "";
		else return StringUtils.toCurrencyNoPoint(Long.parseLong(temp));
	}
	/**
	 * 把一个数字字符 除以10000 但是如果为""则返回""
	 */
	public static String toWAN(String temp){
		if(null == temp || "".equals(temp))return "";
		else return  toCurrencyWithoutComma(Double.parseDouble(temp)*1.0/10000);
	}
	/**
	 * 把一个数字字符 除以100000000 但是如果为""则返回""
	 */
	public static String toYI(String temp){
		if(null == temp || "".equals(temp))return "";
		else return  toCurrencyWithoutComma(Double.parseDouble(temp)*1.0/100000000);
	}
	/**
	 * 百分
	 * @param str
	 * @return
	 */
	public static String turnTobaifen(String str){
		if (str == null) {
			return str;
		}
		if (str .length()==0) {
			return str;
		}
		double result=Double.parseDouble(str);
		DecimalFormat df=new DecimalFormat("0.000");
		result=result*100;
		//return suffixStr(result+"",'0',4);
		return df.format(result);
	}
	
	public static void main(String args[])
	{
		
		System.out.println("2009-03-21".substring(0,4));
		System.out.println(toCurrencyWithoutComma(1.99999999999));
	}
	
}
